const app = getApp();
const https = require('../../utils/https.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiple_file: [],
    current_banner: 1, //商品轮播下标
    tabIndex: 0,
    orderflag: 4, //评论星级
    toView: 't1',
    current2: 0,
    common_count: 0,
    common_list: [],
    buyNumber: 1,
    goods_id: 0,
    goods_name: '魔尺严选优质商品',
    guige_pic: '',
    isOverShare: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.goods_id && options.goods_id > 0) {
      this.setData({
        goods_id: options.goods_id
      })
    }
    wx.getSystemInfo({
      success: (options) => {
        var ht = options.windowHeight; // 页面高度
        this.setData({
          scroll_top: ht
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //检测登陆状态
    this.setData({
      login_status: wx.getStorageSync('login_status'),
      // login_status: false,
      user_id: wx.getStorageSync('user_id')
    })
    this.getInfo();
  },
  /**
   * @xk 获取详情
   */
  getInfo: function () {
    var that = this,
      user_id = wx.getStorageSync('user_id'),
      goods_id = this.data.goods_id;
    https.post('getGoodsInfo', {
      goods_id: goods_id,
      user_id: user_id
    }).then(res => {
      that.setData({
        info: res.info,
        goods_name: res.info.goods_name,
        spec_list: res.spec_list,
        spec_count: res.spec_count,
        guige_pic: res.info.goods_pic,
        goods_attr: res.goods_attr,
        guige_big_price: res.info.sale_big_price,
        guige_small_price: res.info.sale_small_price,
        common_list: res.common_list,
        common_count: res.common_count,
        guige_price: res.info.sale_price,
        guige_value: '请选择商品属性',
        store_count: res.info.store_count,
      })
    })
  },
  /**
   * @xk 规格操作
   * @param  obj 
   */
  spacetap: function (obj) {
    var item_id = obj.currentTarget.dataset.item_id;
    var keyx = obj.currentTarget.dataset.keyx;
    var spec_list = this.data.spec_list;
    var count = spec_list[keyx].length;
    for (var i = 0; i < count; i++) {
      var item = spec_list[keyx][i]['item_id'];
      if (item == item_id) {
        spec_list[keyx][i]['item_select'] = true;
      } else {
        spec_list[keyx][i]['item_select'] = false;
      }
    }
    this.setData({
      spec_list: spec_list
    })
    this.getSpecInfo();
  },
  /**
   * @xk 规格操作
   */
  getSpecInfo: function () {
    var spec_list = this.data.spec_list,
      goods_id = this.data.goods_id,
      buyNumber = this.data.buyNumber,
      spec_count = this.data.spec_count,
      that = this;
    https.post('getSpecPrice', {
      spec_list: spec_list,
      goods_id: goods_id,
      spec_count: spec_count
    }, true).then(res => {
      if (buyNumber > res.store_count) {
        wx.showToast({
          title: '当前库存仅剩' + res.store_count + '件',
          icon: 'none',
          duration: 1500
        })
        buyNumber = res.store_count;
      }
      that.setData({
        guige_pic: res.guige_pic,
        guige_price: res.guige_price,
        guige_big_price: res.guige_big_price,
        guige_small_price: res.guige_small_price,
        guige_value: '已选择' + res.guige_value,
        
        store_count: res.store_count,
        buyNumber: buyNumber,
        spec_key: res.spec_key,
        spec_value: res.guige_value
      })
    })
  },
  // 加
  addTap: function () {
    var buyNumber = this.data.buyNumber + 1,
      store_count = this.data.store_count;
    if (buyNumber > store_count) {
      wx.showToast({
        title: '当前库存仅剩' + store_count + '件',
        icon: 'none',
        duration: 1500
      })
      buyNumber = store_count;
    }
    this.setData({
      buyNumber: buyNumber
    })
  },
  // 减
  reduceTap: function () {
    var buyNumber = this.data.buyNumber - 1;
    if (buyNumber <= 0) {
      wx.showToast({
        title: '最少要购买一件产品哦！',
        icon: 'none',
        duration: 1500
      })
      buyNumber = 1;
    }
    this.setData({
      buyNumber: buyNumber
    })
  },
  /**
   * 购物车展开
   */
  cartshow: function (obj) {
    var type = obj.currentTarget.dataset.type;
    this.setData({
      type: type,
      maskShow: true,
      csmaskShow: false,
      cartShow: true
    })
  },

  /**
   * 商品参数
   */
  onParameter() {
    this.setData({
      maskShow: true,
      csmaskShow: true,
      cartShow: false
    })
  },
  /**
   * 加入购物车
   */
  addCart(obj) {
    var that = this,
        guige_pic = this.data.guige_pic,
        guige_price = this.data.guige_price,
        buy_num = this.data.buyNumber,
        goods_id = this.data.goods_id,
        type = obj.currentTarget.dataset.type,
        spec_key = this.data.spec_key,
        spec_value = this.data.spec_value,
        goods_name = this.data.goods_name,
        user_id = wx.getStorageSync('user_id');
    https.post('saveGoodsCart',{goods_pic:guige_pic,goods_price:guige_price,spec_value:spec_value,buy_num:buy_num,goods_id:goods_id,type:type,spec_key:spec_key,user_id:user_id,goods_name:goods_name},false,'正在提交订单').then(res=>{
        that.maskHide();
        app.globalData.cart_id = res;
        if(type == 'order'){
          wx.navigateTo({
            url: '/pages/goods_sure/goods_sure',
          })
        }else{
          wx.showToast({
            title: '加入购物车成功',
            icon: 'none',
            duration: 1500
          })
        }
    })

  },
  /**
   * 弹窗关闭
   */
  maskHide() {
    this.setData({
      maskShow: false,
      csmaskShow: false,
      cartShow: false
    })
    this.clearSpec();
  },
  /**
   * @xk 清理规格
   */
  clearSpec(){
    var that = this,
        spec_list = this.data.spec_list,
        store_count = this.data.info.store_count,
        guige_price = this.data.info.sale_price,
        guige_pic = this.data.info.goods_pic,
        goods_id = this.data.goods_id,
        guige_big_price = this.data.info.sale_big_price,
        guige_small_price = this.data.info.sale_small_price;
    https.post('clearSpec',{spec_list:spec_list,goods_id:goods_id},true).then(res=>{
      that.setData({
        spec_list: res,
            spec_key: '',
            store_count: store_count,
            guige_pic: guige_pic,
            guige_big_price: guige_big_price,
            guige_small_price: guige_small_price,
            guige_price: guige_price,
            guige_value: '请选择商品属性',
            spec_value: ''
      })
    })
  },
  /**
   * 轮播图滚动
   */
  bannerChange(e) {
    this.setData({
      current_banner: e.detail.current + 1,
      current: e.detail.current
    })
    if (e.detail.current = 1) {
      this.setData({
        isPlay: true
      })
    }
  },
  // tab切换
  tabTap(e) {
    var toView = e.currentTarget.dataset.id
    this.setData({
      tabIndex: e.currentTarget.dataset.index,
      toView: toView,
      isnavfixed: true
    })
  },
  scroll(e) {
    let _top = e.detail.scrollTop;
    let progress = parseInt(_top / this.data.scroll_top); // 计算出 当前的下标
    if (progress > this.data.current2) { // 向上拉动屏幕
      this.setData({
        current2: progress,
        tabIndex: progress
      });
    } else if (progress == this.data.current2) {
      return false;
    } else { // 向下拉动屏幕
      this.setData({
        current2: progress == 0 ? 0 : progress--,
        tabIndex: progress
      });
    }
  },

  /**
   * @xk 调用授权，进行登录
   */
  getUserInfo: function(e){
    var that = this;
    wx.navigateTo({
      url: '/pages/grant/grant',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var timestamp = Date.parse(new Date()),

    info = this.data.info,

    user_id = wx.getStorageSync('user_id'),
    goods_id = this.data.goods_id,
    url = '/pages/home/home',
    timestr = timestamp / 1000;
    url += '?timestr=' + timestr;
   const {guige_pic, goods_name} = this.data;
    if(goods_id > 0){
      url += '&goods_id='+goods_id;
    }
    const s = {
      title: goods_name,
      path: url,
      imageUrl: guige_pic,
      
    }
    console.log(s)
    return s
  },
  onShareTimeline:function (){
    var info = this.data.info,

   
    goods_id = this.data.goods_id,
    url = '/pages/home/home?';
   

    console.info(info)
    if(goods_id > 0){
      url += '&goods_id='+goods_id;
    }
    const s = {
      title: info.goods_name,
      url: url,
      imageUrl: info.goods_pic,
      
    }
    console.log(s)
    return s  
  }
})